<template>
    <div>
        <p class="header_detail">按钮</p>
        <!-- 添加图片 -->
        <div class="detail">
            <p class="detail_explain">· 添加图片，建议图片尺寸为宽750像素，最高不超过1536像素；</p>
            <p class="detail_explain">· 大小不超过300KB；</p>
        </div>
        <div class="detail_set">
            <div class="addBtns" @click='addBtnList'>
                <span>添加按钮({{data.btnList.length}}/2)</span>
            </div>
            <div class="btnDetail" v-for='(item,index) in data.btnList' :key='index'>
                <div class="delete" @click="deleteBtn(index)"></div>
                <p class="form_title2">按钮{{index+1}}</p>
                <el-form ref="form" label-position="left"  :model="data" label-width="80px">
                    <el-form-item label='按钮类型：'>
                        <el-radio-group v-model="data.borderType" @change="toParent">
                            <el-radio label="1">边框</el-radio>
                            <el-radio label="2">填充</el-radio>
                        </el-radio-group>
                        <el-color-picker class="colorPicker" v-model="data.bgOrBorderColor " @change='toParent'></el-color-picker>
                    </el-form-item>
                    <el-form-item label='文本内容：'>
                        <el-input v-model='item.textContent'  @change="toParent"  ></el-input>
                    </el-form-item>
                    <el-form-item label='文字字体：'>
                        <el-select v-model='item.textFontFamily' @change='toParent'>
                            <el-option value='Microsoft YaHei' label='微软雅黑'></el-option>
                            <el-option value='SimHei' label='黑体'></el-option>
                            <el-option value='SimSun' label='宋体'></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label='文本字号：' >
                        <el-select v-model='item.fontSize'  @change='toParent'>
                            <el-option v-for='(item,index) in fontOption' :label='item.label' :value='item.value' :key='index'></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item :label='`文本颜色：${item.textColor}`' label-width="160px" >
                        <el-color-picker v-model="item.textColor" @change='toParent'></el-color-picker>
                    </el-form-item>
                    <el-form-item :label='`背景颜色：${item.bgColor}`' label-width="160px" >
                        <el-color-picker v-model="item.bgColor" @change='toParent'></el-color-picker>
                    </el-form-item>
                    <!-- <el-form-item label='显示位置：'>
                        <el-radio-group v-model="data.textAlign" @change="toParent">
                            <el-radio label="left">居左</el-radio>
                            <el-radio label="center">居中</el-radio>
                            <el-radio label="right">居右</el-radio>
                        </el-radio-group>
                    </el-form-item> -->
                    <!-- <el-form-item label='跳转链接：'>
                        <el-input v-model='item.jumpUrl'  @change="toParent"  ></el-input>
                    </el-form-item> -->
                    <el-form-item label='跳转链接：' label-width="90px">
                        <c-linkselect v-model="item.wxJumpUrl"  @change="toParent"></c-linkselect >
                        <!-- <el-input v-model='item.wxJumpUrl'  @change="toParent"  ></el-input> -->
                    </el-form-item>
                </el-form>
            </div>
            <div class="block">
                <p class="form_title">按钮下边距  <span>(0为贴合，最大100像素)</span></p>
                <el-slider
                    v-model="data.bottom"
                    show-input
                    :max='100'
                    :step="1"
                    @change='toParent'>
                </el-slider>
            </div>
            <!-- 上下边距-->
            <div class="block">
                <p class="form_title">上边框距离  <span>(0为贴合，最大100像素)</span></p>
                <el-slider
                    v-model="data.paddingTop"
                    show-input
                    :max='100'
                    :step="1"
                    @change='toParent'>
                </el-slider>
            </div>
            <div class="block">
                <p class="form_title">下边框距离  <span>(0为贴合，最大100像素)</span></p>
                <el-slider
                    v-model="data.paddingBottom"
                    show-input
                    :max='100'
                    :step="1"
                    @change='toParent'>
                </el-slider>
            </div>
        </div>
        <div  class="eventBus">
            <p class="event_title" >事件（选填）</p>
            <el-input @change='toParent' v-model='data.event' :disabled="data.eventChecked" placeholder="请输入事件ID"></el-input>
        </div>
        
    </div>
</template>

<script>
export default {
    data(){
        return {
            data:{
                btnList:[]
            },
            imgHeight:200,
        }
    },
    props:{
        propData:{
            type:Object
        }
    },
    methods:{
        clearImg(){
            this.data.imageUrl = '';
            this.toParent();
        },
        uploadImg(){

        },
        handleRemove(){

        },
        getIndex(val){
        },
        getImg(val){
            let that = this;
            let infoId = this.$route.query.id;
            var file = val.raw;
            var formdata=new FormData();// 创建form对象
            formdata.append('file',file,file.name);
            this.$ajax({
                method:'post',
                url:`/base/systemFile/insertCmsPictureAndMp4/${infoId}`,
                data:formdata
            }).then(res=>{
                if(res.data.status==200){
                    var image = new Image()
                    image.src = res.data.result.url;
                    that.data.imageUrl= res.data.result.url;
                    image.onload =function(){
                        var naturalWidth = image.width;
                        var  naturalHeight = image.height*375/naturalWidth;
                        if(that.imgHeight < naturalHeight){
                            that.data.imgHeight = naturalHeight;
                            that.imgHeight = naturalHeight;
                        }
                        that.toParent();
                    }
                }
            }).catch(err=>{
                this.$message.error('上传异常，请重试')
            })
            // var reader = new FileReader();
            // this.$ajax({
            //     method:'post',
            //     url:'/base/systemFile/insertCmsPictureAndMp4',
            //     data:file
            // })
            // reader.readAsDataURL(file);
            // reader.onload =(e)=>{ 
            //     that.data.imageUrl=e.target.result;
            //     var image = new Image()
            //     image.src = e.target.result
            //     image.onload =function(){
            //         var naturalWidth = image.width;
            //         var  naturalHeight = image.height*375/naturalWidth;
            //         if(that.imgHeight < naturalHeight){
            //             that.data.imgHeight = naturalHeight;
            //             that.imgHeight = naturalHeight;
            //         }
            //         that.toParent();
            //     }
            // }
        },
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isLt2M = file.size / 1024 / 1024 < 2;

            if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
            }
            if (!isLt2M) {
            this.$message.error('上传头像图片大小不能超过 2MB!');
            }
            return isJPG && isLt2M;
        },
        addBtnList(){
            if(this.data.btnList.length<2 ){
                this.data.btnList.push({
                    bgColor: "#8b776c",
                    fontSize: 28,
                    textColor: "#FFFFFF",
                    textContent: "立即预约",
                    textFontFamily: "Microsoft YaHei",
                });
                this.toParent();
            }else{
                return ;
            }
        },
        deleteBtn(index){
            this.data.btnList.splice(index,1);
            this.toParent();
        },
        toParent(){
            let data = {
                ...this.data
            }
            this.$emit('toParent',data)
        }
    },
    watch:{
        propData:function(val){
           this.data = JSON.parse(JSON.stringify(this.propData)); 
        }
    },
    mounted(){
        this.fontOption = [];
        for(let i=24;i<84;i=i+4){
            this.fontOption.push({
                label:i,
                value:i
            })
        }
        this.data = JSON.parse(JSON.stringify(this.propData)); 
        this.toParent();
        console.log(this.propData,'接收到的参数')
        // debugger;
    }    

}
</script>
<style lang="less" scoped>
    .header_detail{
        height:50px;
        line-height:50px;
        // border-bottom:1px solid #E4E4E4;
        font-size: 16px;
        color: #000000;
        padding-left:16px;
    }
    .detail{
        padding: 16px 16px 0 16px;
        border-top: 1px solid #E4E4E4;
        font-size: 14px;
        .btnDetail{
            border-bottom:1px solid #E4E4E4;
            padding: 16px 16px 0 25px;
            position:relative;
        }
        .form_title{
            position:relative;
            left:-10px;
            margin-top: 10px;
            span{
                color: #888888;
            }
        } 
        .detail_explain{
            font-size: 12px;
            line-height: 17px;
            color: #888888;
        }
        .detail_content{
            width:100%;
            ul{
                width:100%;
                height:100%;
            }
            li{
                width:100%;
                margin-top: 20px;
            }
            .imgBox{
                width:100%;
                height:100%;
                display:flex;
                justify-content: space-between;
                align-items: center;
                .left{
                    width:150px;
                    height:100px;
                    display:flex;
                    justify-content: center;
                    align-items: center;
                    .left_btns{
                        width:50px;
                        height:100%;
                    }
                    .left_imgs{
                        width:100px;
                        height:100%;
                        background:gray;
                    }
                }
                .center{
                    width:250px;
                    height:100px;
                }
                .right{
                    width:60px;
                    height:auto;
                    &:hover{
                        color:red;
                    }
                }
            }
            .addBtns{
                text-align:center;
                line-height:30px;
                span{
                    color:blue;
                    
                }
            }
        }
    }
    .uploadImg{
        width:100%;
        // height:320px;
        position:relative;
        padding:24px 16px 17px 16px;
        margin-top:16px;
        box-sizing: border-box;
        background:#F7F7F7;
        line-height:35px;
        font-size:14px;
        color:#666666;
        .delete{
            width:14px;
            height:14px;
            position:absolute;
            top: 8px;
            right:6px;
            z-index: 10;
            background:url('~@/assets/img/editContent/icon-ashbin-nor.png') center center/14px 14px  no-repeat;
        }
        .delete:hover{
            background:url('~@/assets/img/editContent/icon-ashbin-hov.png') center center/14px 14px  no-repeat;                    
        }
         /deep/ .avatar-uploader .el-upload {
            border: 1px dashed #FF8244;
            box-sizing: border-box;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
            width:100%;
            height:140px;         
            display:flex;
            justify-content: center;
            align-items:center;
            flex-direction: column;
            color:#FF8244;
        }
        /deep/ .avatar-uploader .el-upload:hover {
            // border-color: #FF8244;
        }
        /deep/.avatar-uploader-icon {
            font-size: 22px;  
            color:#FF8244;
        }
        /deep/.avatar {
            width:100%;
            height:140px;
            display: block;
        }
    }
    /deep/.el-slider__bar{
        background-color:#FF8244;
    }
    /deep/.el-slider__button{
        border: 2px solid #FF8244;
    }
    .eventBus{
        font-size: 14px;
        padding:5px 16px 0 16px;
        margin-bottom:40px;
        .event_title{
           margin-bottom:10px;
        }
    }
    .detail_set{
        font-size: 14px;
    }
    .el-form-item {
        margin: 10px 0 10px 0;
        height: auto;
        display: block;
    }
    .block{
        padding: 16px 16px 0 25px;
    }
    .addBtns {
        width: calc(100% - 32px);
        margin-left: 16px;
        text-align: center;
        line-height: 40px;
        border: 1px solid #ff8244;
        box-sizing: border-box;
        color: #ff8244;
        font-size: 14px;
        margin-top: 10px;
        cursor: pointer;
    }
    /deep/.el-color-picker__trigger{
        height:30px;
    }
    .delete {
        width: 14px;
        height: 14px;
        position: absolute;
        top: 12px;
        right: 16px;
        background: url("~@/assets/img/editContent/icon-ashbin-nor.png")
            center center/14px 14px no-repeat;
            cursor: pointer;
    }
    .delete:hover{
        background:url('~@/assets/img/editContent/icon-ashbin-hov.png') center center/14px 14px  no-repeat;                    
    }
    .colorPicker{
        margin-left:20px;
        vertical-align: top;
    }
    .el-radio{
        margin-right:10px;
    }
</style>